package com.hanxiaozhang.util;

import cn.hutool.core.util.CharUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

/**
 * 功能描述: <br>
 * 〈文件工具类〉
 *
 * @Author:hanxinghua
 * @Date: 2021/5/2
 */
public class FileUtil {


    /**
     * 类Unix路径分隔符
     */
    public static final char UNIX_SEPARATOR = CharUtil.SLASH;

    /**
     * Windows路径分隔符
     */
    public static final char WINDOWS_SEPARATOR = CharUtil.BACKSLASH;

    /**
     * 是否为Windows环境
     */
    public static boolean isWindows() {
        return WINDOWS_SEPARATOR == File.separatorChar;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param filePath
     * @param fileName
     * @throws Exception
     */
    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }


    /**
     * 删除文件
     *
     * @param fileName
     * @return
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


    /**
     * 文件名添加uuid
     *
     * @param preName
     * @param extName
     * @return
     */
    public static String fileNamePlusUUID(String preName, String extName) {
        return preName + "_" + UUID.randomUUID() + "." + extName;
    }

}
